package tienda.zapateria.siszap.da;

import java.sql.Connection;
import java.util.ArrayList;
import org.rx.cr.dao.DAOAbstract;
import tienda.zapateria.siszap.be.BERelacionTalla;

public class DARelacionTalla extends DAOAbstract<BERelacionTalla> {

    public DARelacionTalla(Connection c) {
        setConnection(c);
    }

    @Override
    public int registrar(BERelacionTalla be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_ins_relaciontalla");
            setParameterInt(be.getArticuloid());
            setParameterInt(be.getTallaid());
            setParameterInt(be.getStock());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int actualizarRegistro(BERelacionTalla be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_upd_relaciontalla");
            setParameterInt(be.getRelaciontallaid());
            setParameterInt(be.getArticuloid());
            setParameterInt(be.getTallaid());
            setParameterInt(be.getStock());

            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public int eliminarRegistro(BERelacionTalla be) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_relaciontalla");
            setParameterInt(be.getRelaciontallaid());
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    @Override
    public ArrayList<BERelacionTalla> buscarReferencia(String string) throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_relaciontalla");
            setParameterString(string);
            commitc();
            while (existResult()) {
                BERelacionTalla be = new BERelacionTalla();
                be.setRelaciontallaid(getDataInt(1));
                be.setArticuloid(getDataInt(2));
                be.setTallaid(getDataInt(3));
                be.setStock(getDataInt(4));

                be.setTipo(getDataString(5));
                be.setMarca(getDataString(6));
                be.setColor(getDataString(7));
                be.setMaterial(getDataString(8));
                be.setPreciocompra(getDataDouble(9));
                be.setPrecioventa(getDataDouble(10));

                be.setCategoria(getDataString(11));
                be.setEuropea(getDataString(12));
                be.setAmericana(getDataString(13));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public ArrayList<BERelacionTalla> listar() throws Exception {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_relaciontalla");
            setParameterString("");
            commitc();
            while (existResult()) {
                BERelacionTalla be = new BERelacionTalla();
                be.setRelaciontallaid(getDataInt(1));
                be.setArticuloid(getDataInt(2));
                be.setTallaid(getDataInt(3));

               be.setStock(getDataInt(4));

                be.setTipo(getDataString(5));
                be.setMarca(getDataString(6));
                be.setColor(getDataString(7));
                be.setMaterial(getDataString(8));
                be.setPreciocompra(getDataDouble(9));
                be.setPrecioventa(getDataDouble(10));

                be.setCategoria(getDataString(11));
                be.setEuropea(getDataString(12));
                be.setAmericana(getDataString(13));

                add(be);
            }
            return getLista();
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    @Override
    public BERelacionTalla buscar(int i) throws Exception {
        try {
            BERelacionTalla be = null;
            clear();
            setStoreProcedure("zapateria.sp_sel_relaciontalla_id");
            setParameterInt(i);
            commitc();
            while (existResult()) {
                be = new BERelacionTalla();
                be.setRelaciontallaid(getDataInt(1));
                be.setArticuloid(getDataInt(2));
                be.setTallaid(getDataInt(3));

                be.setStock(getDataInt(4));

                be.setTipo(getDataString(5));
                be.setMarca(getDataString(6));
                be.setColor(getDataString(7));
                be.setMaterial(getDataString(8));
                be.setPreciocompra(getDataDouble(9));
                be.setPrecioventa(getDataDouble(10));

                be.setCategoria(getDataString(11));
                be.setEuropea(getDataString(12));
                be.setAmericana(getDataString(13));

            }
            return be;
        } catch (Exception e) {
            rollback(e);
            return null;
        }
    }

    public int eliminarAsignacion(int distribucionId) throws Exception {
        try {
            setStoreProcedure("zapateria.sp_del_asignacionrelaciontalla");
            setParameterInt(distribucionId);
            return Integer.parseInt(commit().toString());
        } catch (Exception e) {
            return rollback(e);
        }
    }

    public ArrayList<BERelacionTalla> listarAsignacion(int propiedadid) {
        try {
            clear();
            setStoreProcedure("zapateria.sp_sel_asignacion_asignacionrelaciontalla");
            setParameterInt(propiedadid);
            commitc();
            while (existResult()) {
                BERelacionTalla be = new BERelacionTalla();
                be.setRelaciontallaid(getDataInt(1));
                be.setArticuloid(getDataInt(2));
                be.setTallaid(getDataInt(3));
                be.setStock(getDataInt(4));
                add(be);

            }
            return getLista();

        } catch (Exception e) {
            try {
                rollback(e);
            } catch (Exception ex) {
            }
            return null;
        }
    }

}
